package com.ls.controller.weixin;

import com.ls.controller.weixin.model.WeixinModel;
import com.ls.entity.OrderRecord;
import com.ls.service.IOrderRecordService;
import com.ls.service.IWeixinPayService;
import com.ls.utils.ResultData;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.Map;

import static com.ls.constant.SystemConstants.PAY_ALIPAY;
import static com.ls.constant.SystemConstants.PAY_WEIXIN;

/**
 * <p>
 * 微信支付 前端控制器
 * </p>
 *
 * @author pengyu
 * @since 2024-10-12
 */
@Api(value = "微信app支付相关接口", tags = "微信app支付相关接口")
@Slf4j
@RestController
@RequestMapping("/weixinApp")
@RequiredArgsConstructor
public class WeixinPayAppController {
    private final IWeixinPayService service;
    private final IOrderRecordService orderRecordService;
    /** 预支付 */
    @PostMapping("/advancePay")
    @ResponseBody
    public ResultData advancePay(@Valid @RequestBody WeixinModel model) {
        OrderRecord orderRecord = orderRecordService.getOrderRecordByOrderNum(model.getOrderNum());
        log.info("订单号预支付:{}",orderRecord);
        orderRecord.setPayType(PAY_WEIXIN);
        orderRecord.updateById();
        return service.advancePay(model.getOrderNum(),orderRecord.getFee(), model.getProductType());
    }
    /** 支付结果异步回调 */
    @PostMapping("/notify")
    public String paymentNotify(@RequestParam HttpServletRequest request) {
        log.info("微信支付回调");
        return service.getPayType(request);
    }
    /** 统一查询支付接口 */
    @GetMapping("/getPayStatus")
    public ResultData getPayStatus(String orderNum) {
        return service.getPayStatus(orderNum);
    }
}
